package com.aim.analysis.artifact;

import com.aim.analysis.records.Hit;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.EnumSet;

/**
 * User: Avraham Shvartzon
 * Date: 7/24/14
 */
public abstract class AbstractArtifact {

    protected EnumSet<ExperimentDate> experimentDates;
    protected Collection<Hit> hitCollection;
    private File rootFolder;

    public void setRootFolder(File rootFolder) {
        this.rootFolder = rootFolder;
    }

    public abstract ArtifactSubFolder getArtifactSubFolder();

    protected File getArtifactFolder(){
        File artifactFolder = new File(rootFolder, getArtifactSubFolder().subFolder);
        if (artifactFolder.isDirectory() == false){
            artifactFolder.mkdirs();
        }
        return artifactFolder;
    }


    public abstract boolean execute(Collection<Hit> hitCollection) throws Exception;

    protected boolean writeToFile(XSSFWorkbook workbook) throws IOException {
        File xlsFile = new File(getArtifactFolder(), getArtifactSubFolder().toString() + "_" + getFileNameSuffix() + ".xlsx");

        FileOutputStream stream = new FileOutputStream(xlsFile);
        workbook.write(stream);
        stream.close();
        return true;
    }

    protected abstract String getFileNameSuffix();
}
